﻿using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using ZBJF.Warranty.Common.Common;
using ZBJF.Warranty.Common.Enums.Base;
using ZBJF.Warranty.Common.Enums.InfoCollect;
using ZBJF.Warranty.Common.Enums.InsuranceProducts;
using ZBJF.Warranty.Model;

namespace ZBJF.Warranty.BLL
{
    public partial class InsuranceProductInfoCollectItemSnapshootService
    {
        /// <summary>
        /// 创建资料收集快照
        /// </summary>
        /// <param name="insuranceOrderId">保单表InsuranceOrder主键ID</param>
        /// <param name="serviceProjectType">保险信息类型：和ServiceProductType是同一个枚举，根据ServiceProjectType去不同的表取数据，比如手机维修保险信息，就去CellPhoneInsuranceInfo表取</param>
        /// <returns></returns>
        public bool CreateSnapshoot(long insuranceOrderId, ServiceProductType serviceProjectType)
        {
            var result = true;
            var cellPhoneInsuranceInfoList = ServiceFactory.CellPhoneInsuranceInfoServiceInstance.LoadByInsuranceID(insuranceOrderId);//todo 根据ServiceProjectType去不同的表取数据
            if (cellPhoneInsuranceInfoList.Any())
            {
                var cellPhoneInsuranceInfoIDList = cellPhoneInsuranceInfoList.Select(o => o.CellPhoneServiceProjectID.Value).ToList();//存放保险性信息的主键IDs
                var infoCollectItemList =
                    ServiceFactory.InsuranceProductsInfoCollectItemMappingsServiceInstance.LoadInfoCollectItems(cellPhoneInsuranceInfoIDList).ToList();
                if (infoCollectItemList.Any())
                {
                    for (int i = 0; i < infoCollectItemList.Count; i++)
                    {
                        if (!result)
                            return false;
                        var infoCollectItem = infoCollectItemList[i];
                        var entity = new InsuranceProductInfoCollectItemSnapshoot
                        {
                            InsuranceOrderServiceProjectId = infoCollectItem.InsuranceOrderServiceProjectId,
                            ServiceProjectType = (byte)serviceProjectType,
                            InfoCollectWith = infoCollectItem.InfoCollectWith,
                            InfoCollectWithName = infoCollectItem.InfoCollectWithName,
                            InfoCollectCategoryName = infoCollectItem.InfoCollectCategoryName,
                            InfoCollectItemID = infoCollectItem.InfoCollectItemID,
                            InfoCollectItemName = infoCollectItem.InfoCollectItemName,
                            IsMust = infoCollectItem.IsMust,
                            CanUploadPicCount = infoCollectItem.CanUploadPicCount
                        };
                        result = AddEntities(entity).ID > 0;
                    }
                }
            }
            return result;
        }
        /// <summary>
        /// 获取信息收集项
        /// </summary>
        /// <param name="insuranceOrderServiceProjectId"></param>
        /// <param name="serviceProjectType"></param>
        /// <param name="infoCollectWith"></param>
        /// <returns></returns>
        public IQueryable<InsuranceProductInfoCollectItemSnapshoot> GetInsuranceProductInfoCollectItemSnapshoot(long insuranceOrderServiceProjectId, ServiceProductType serviceProjectType, byte infoCollectWith = 0)
        {
            var byteServiceProductType = (byte)serviceProjectType;
            var predicate = PredicateExtensions.True<InsuranceProductInfoCollectItemSnapshoot>().And(o => o.InsuranceOrderServiceProjectId == insuranceOrderServiceProjectId && o.ServiceProjectType == byteServiceProductType);
            if (infoCollectWith > 0)
                predicate = predicate.And(o => o.InfoCollectWith == infoCollectWith);
            return LoadEntities(predicate);
        }
    }
}